import { Connection } from 'mongoose';
import { getInstance } from '../../db';
import { commentSchema } from '../../schemas/commentSchema';
import { countTotal, limitPage } from '../../../util/dbOpt';

export const getCommentListService = async (params: IAnyObject): Promise<IAnyObject> => {
  const db: Connection = await getInstance();
  const commentModel = db.model('comment', commentSchema);

  const { videoId, pageSize, pageNum } = params;

  const queryCondition = {
    videoId: {
      $eq: videoId,
    },
  }

  // 排序，暂不需要
  const sortCondition = {
    // createTime: 1,  // 创建时间升序排列
  }

  // 总数
  const total = await countTotal(commentModel ,queryCondition);

  // 分页
  const dataList = await limitPage(
    commentModel,
    pageSize,
    pageNum,
    queryCondition,
    sortCondition
  );


  const result = {
    list: dataList,
    pageSize,
    pageNum,
    total,
  }

  return result;
}